using System;
using NUnit.Framework;
using ValidationFramework.Configuration;
using ValidationFramework.Extensions;

namespace ValidationFramework.Tests.Configuration
{
    [TestFixture]
    public class RequiredEnumRuleConfigReaderTest
    {
        public enum Numbers
        {
            One,
            Two,
            Three
        }
        #region Methods

        [Test]
        public void ReadConfig()
        {
            const string validatorXml = "<rule errorMessage='hello' initialValue='Two' typeName='RequiredEnumRule' ruleSet='foo' />";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Numbers).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            Assert.AreEqual(Numbers.Two, rule.InitialValue);


        }

        [Test]
        public void ReadConfigSimpleNotNullable()
        {
            const string validatorXml = "<rule typeName='RequiredEnumRule'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Numbers).TypeHandle);
            Assert.AreEqual(Numbers.One, rule.InitialValue);
            Assert.IsNull(rule.ErrorMessage);
        }


        [Test]
        public void ReadConfigSimpleNullable()
        {
            const string validatorXml = "<rule typeName='RequiredEnumRule'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Numbers?).TypeHandle);
            Assert.IsFalse(rule.HasInitialValue);
            Assert.IsNull(rule.ErrorMessage);
        }

        [Test]
		[ExpectedException(typeof(ArgumentException),ExpectedMessage = "Incorrect type to be validated. Should be an enum not an instance of Enum\r\nParameter name: runtimeTypeHandle" )]
        public void ExceptionReadConfigEnum()
        {
            const string validatorXml = "<rule typeName='RequiredEnumRule'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Enum).TypeHandle);
            Assert.IsFalse(rule.HasInitialValue);
            Assert.IsNull(rule.ErrorMessage);
        }
        [Test]
        public void ReadConfigSimpleNotNullableInitialValue()
        {
            const string validatorXml = "<rule typeName='RequiredEnumRule' initialValue='Two'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var requiredEnumRule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Numbers).TypeHandle);
            Assert.AreEqual(Numbers.Two, requiredEnumRule.InitialValue);
            Assert.IsNull(requiredEnumRule.ErrorMessage);
        }


        [Test]
        public void ReadConfigSimpleNullableInitialValue()
        {
            const string validatorXml = "<rule typeName='RequiredEnumRule' initialValue='Two'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Numbers>)ConfigurationService.GetRule(ruleData, typeof(Numbers?).TypeHandle);
            Assert.AreEqual(Numbers.Two, rule.InitialValue);
            Assert.IsNull(rule.ErrorMessage);
        }

        #endregion


    }
}